/* Copyright 2009 - 2010 The Stajistics Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.stajistics.integration.servlet;

import static org.stajistics.Util.assertNotNull;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.Writer;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletResponseWrapper;

import org.stajistics.StatsFactory;
import org.stajistics.StatsKey;
import org.stajistics.io.StatsFilterWriter;

/**
 * 
 * @author The Stajistics Project
 */
public class StatsHttpServletResponse extends HttpServletResponseWrapper {

    private int statusCode = SC_OK;

    private final StatsFactory statsFactory;
    private final StatsKey responseStreamKey;

    private ServletOutputStream statsOutputStream;
    private PrintWriter statsWriter;

    public StatsHttpServletResponse(final HttpServletResponse response,
                                    final StatsFactory statsFactory,
                                    final StatsKey responseStreamKey) {
        super(response);

        assertNotNull(statsFactory, "statsFactory");

        this.statsFactory = statsFactory;
        this.responseStreamKey = responseStreamKey;
    }

    @Override
    public void setStatus(final int statusCode) {
        super.setStatus(statusCode);
        this.statusCode = statusCode;
    }

    public int getStatus() {
        return statusCode;
    }

    @Override
    public void reset() {
        super.reset();
        this.statusCode = SC_OK;
    }

    protected ServletOutputStream wrapOutputStream(final ServletOutputStream out) {
        return new StatsServletOutputStream(statsFactory, responseStreamKey, out);
    }

    protected PrintWriter wrapWriter(final Writer writer) {
        return new PrintWriter(new StatsFilterWriter(statsFactory, responseStreamKey, writer));
    }

    @Override
    public ServletOutputStream getOutputStream() throws IOException {
        if (responseStreamKey != null) {
            if (statsOutputStream == null) {
                statsOutputStream = wrapOutputStream(super.getOutputStream()); 
            }
            return statsOutputStream;
        }
        return super.getOutputStream();
    }

    @Override
    public PrintWriter getWriter() throws IOException {
        if (responseStreamKey != null) {
            if (statsWriter == null) {
                statsWriter = wrapWriter(super.getWriter());
            }
            return statsWriter;
        }
        return super.getWriter();
    }
    
}
